<template>
  <div>
    <zeda-dialog
      v-model="visible"
      width="500px"
      title="升级组件"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="model" :rules="rules" label-width="100px">
        <el-form-item label="版本号" prop="versionId">
          <el-select v-model="model.versionId" placeholder="请选择版本号">
            <el-option
              v-for="item in versionOptions"
              :key="item.id"
              :label="item.dictName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <form-button
        slot="footer"
        cancel-text="关闭"
        @submit="submitForm"
        @cancel="handleClose"
      ></form-button>
    </zeda-dialog>
  </div>
</template>
    <script>
import { validList, emeterUpdate } from "@/api/iot/upgrade";
import FormButton from "@/components/FormButton";
export default {
  components: {
    FormButton,
  },
  props: {
    visible: { type: Boolean, default: false },
    data: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      model: { versionId: "" },
      versionOptions: [],
      rules: {
        versionId: [
          {
            required: true,
            message: "请选择版本号",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  watch: {
    visible(value) {
      if (value) {
        this.getInfo();
      }
    },
  },
  methods: {
    getInfo() {
      validList()
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.versionOptions =
            res.data && res.data.length > 0
              ? res.data.map((item) => {
                  item.dictName = item.fileName + " / " + item.versionCode;
                  return item;
                })
              : [];
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!300705");
        });
    },
    // 提交上传文件
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let list = [];
          for (let i = 0; i < this.data.length; i++) {
            list.push({
              id: this.model.versionId,
              deviceCode: this.data[i].deviceCode,
              monitoringNetworkId: this.data[i].monitoringNetworkId,
            });
          }
          emeterUpdate({ list })
            .then((res) => {
              if (res.code === 700) {
                this.$message.warning(res.msg);
                return false;
              }
              this.$message.success("操作成功");
              this.handleClose(true);
            })
            .catch(() => {
              // this.$message.error("系统错误,请联系管理员!300704");
            });
        }
      });
    },
    handleClose(status) {
      this.$refs["form"].resetFields();
      this.$emit("update:visible", false);
      this.$emit("close", typeof status === "boolean" ? status : false);
    },
  },
};
</script>
